package com.groupfive.controller;

import com.github.pagehelper.PageInfo;
import com.groupfive.common.Result;
import com.groupfive.exception.CustomException;
import com.groupfive.feign.client.DeptClient;
import com.groupfive.pojo.Account;
import com.groupfive.pojo.Dept;
import com.groupfive.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 添加
     */
    @PostMapping("/add")
    public Result add(@RequestBody Dept dept){
        int count = deptService.add(dept);
        if (count == 0 ){
            return Result.error();
        }
        return Result.success();
    }

    /**
     * 更新数据
     */
    @PutMapping("/update")
    public Result update(@RequestBody Dept dept){
        int count = deptService.update(dept);
        if (count == 0 ){
            return Result.error();
        }
        return Result.success();
    }

    /**
     * 删除数据
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id){
        int count = deptService.deleteById(id);
        if (count == 0 ){
            return Result.error();
        }
        return Result.success();
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids){
       deptService.deleteBatch(ids);
       return Result.success();
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Dept dept){
        List<Dept> list = deptService.selectAll(dept);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id")Long id){
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Dept dept,
                             @RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize){
        PageInfo<Dept> pageInfo = deptService.selectPage(dept, pageNum, pageSize);
        return Result.success(pageInfo);
    }
}
